package com.heba.greenmulu.diary;

import android.os.Bundle;
import androidx.viewpager.widget.ViewPager;
import android.view.View;

import com.gyf.immersionbar.ImmersionBar;
import com.heba.adapter.diary.DiaryImageShowPagerAdapter;
import com.heba.banner.ViewPagerFocusView;
import com.heba.bean.diary.ItemDiaryImageObject;
import com.heba.config.CommonConfig;
import com.heba.greenmulu.BaseActivity;
import com.heba.greenmulu.R;

import java.util.List;

/**
 * 显示日记大图viewpager
 * Created by jason on 2020/3/20.
 */
public class ItemDiaryImageShowActivity extends BaseActivity {

    private ViewPager viewPager;
    private ViewPagerFocusView focusView;

    private DiaryImageShowPagerAdapter adapter;

    private List<ItemDiaryImageObject> list;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diary_image_show);
        /**
         * 设置狀態欄顏色
         */
        ImmersionBar.with(this)
                .statusBarColor(R.color.colorTextLogIn)
                .init();
        initTopbar();
        findViewById();
        initView();
        initViewpager();
    }

    @Override
    protected void findViewById() {
        viewPager = findViewById(R.id.viewPager);
        focusView = findViewById(R.id.viewpger_focusview);
    }

    @Override
    protected void initView() {
        txt_title.setText(getResources().getString(R.string.diary_image_show));
        txt_right.setVisibility(View.INVISIBLE);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            list = (List<ItemDiaryImageObject>) bundle.getSerializable(CommonConfig.DIARY_IMAGE_LIST);
            position = bundle.getInt(CommonConfig.POSITION, 0);
        }

        img_back.setOnClickListener(v -> finishActivityWithFadeInAnimation());
    }

    /**
     * initViewpager
     */
    private void initViewpager() {
        adapter = new DiaryImageShowPagerAdapter(this, list);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                focusView.setCurrentIndex(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        if (!list.isEmpty()) {
            //总的点数
            focusView.setCount(list.size(), R.drawable.icon_diary_slideshow_u, R.drawable.icon_diary_slideshow_s);
            //当前位置
            viewPager.setCurrentItem(position);
            focusView.setCurrentIndex(position);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finishActivityWithFadeInAnimation();
    }
}
